package com.mezz.bones.framework.base.domain.response;

import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

/**
 * @author mezz
 * @date 2022-04-15 13:57
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult<T> implements Serializable {

  /*序列化标识*/
  private static final long serialVersionUID = 1L;

  /*业务代码*/
  private String code = ResponseCode.SUCCESS.getCode();;
  /*提示语*/
  private String message = ResponseCode.SUCCESS.getMessage();;
  /*分页数据对象*/
  private PageInfo<T> data;

  @ApiModelProperty(value = "扩展属性")
  private Object extend;

  /**
   * 时间戳
   */
  @ApiModelProperty(value = "时间戳")
  private Long timestamp = System.currentTimeMillis();

  public PageResult(String code, String message) {
    this.code = code;
    this.message = message;
  }

  public static <T> PageResult<T> ofCode(ResponseCode responseCode) {
    return new PageResult<>(responseCode.getCode(), responseCode.getMessage());
  }

  /**
   * 返回成功消息
   */
  public static <T> PageResult<T> success() {
    return ofCode(ResponseCode.SUCCESS);
  }

  public static <T> PageResult<T> success(PageInfo<T> data) {
    PageResult<T> result = ofCode(ResponseCode.SUCCESS);
    result.setData(data);
    return result;
  }

  public static <T> PageResult<T> success(PageInfo<T> data, Object extend) {
    PageResult<T> result = ofCode(ResponseCode.SUCCESS);
    result.setData(data);
    result.setExtend(extend);
    return result;
  }

  /**
   * 设置扩展属性信息
   *
   * @param extend
   * @return
   */
  public PageResult<T> extend(Object extend){
    this.extend = extend;
    return this;
  }
}
